package com.stylefeng.guns.modular.service.impl;

import com.stylefeng.guns.modular.service.MailService;
import com.stylefeng.guns.modular.service.entity.EmailMessage;
import org.springframework.stereotype.Service;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

@Service
public class MailServiceImpl implements MailService {

    private static String senderAddress = "15258883309@163.com";
    private static String senderAccount = "15258883309@163.com";
    private static String senderPassword = "qiubo123";

    private static String LIUQH = "66263346@qq.com";
    private static String SHIYP = "66263346@qq.com";
    private static String YANQB = "66263346@qq.com";

    @Override
    public void sendEmail(EmailMessage message) {

        // 设置收件人和内容
        message = getSendMessage(message);

        try {
            //1、连接邮件参数、回话对象Session
            Properties props = new Properties();
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.transport.protocol", "smtp");
            String mailServerHost = "smtp.163.com";
            props.setProperty("mail.smtp.host", mailServerHost);
            Session session = Session.getInstance(props);
            session.setDebug(true);

            //2、创建邮件的实例对象
            Message msg = getMimeMessage(session, message);

            //3、传输对象Transport、发送邮件，message.getAllRecipients() 获取所有收件人, 抄送人, 密送人
            Transport transport = session.getTransport();
            transport.connect(senderAccount, senderPassword);
            transport.sendMessage(msg,msg.getAllRecipients());

            transport.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建一封邮件的实例对象
     */
    private static MimeMessage getMimeMessage(Session session, EmailMessage message) throws Exception{

        MimeMessage msg = new MimeMessage(session);

        //设置发件人、收件人地址、邮件主题、正文、发送时间。（其中收件人需要遍历设置，内容需要组装）
        msg.setFrom(new InternetAddress(senderAddress,"旭宏办公", "utf-8"));
        msg.setSubject("报销申请","UTF-8");
        msg.setSentDate(new Date());

        String[] receivers = message.getReceivers().split(",");
        for(String receiver:receivers){
            msg.addRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiver,"test", "utf-8"));
        }

        msg.setContent(message.getContent(), "text/html;charset=UTF-8");

        msg.saveChanges();
        return msg;
    }

    /**
     * 分为四部处理报销流程（1、业务经理收， 2、人事经理收 3、总审核 4、放款）
     */
    private EmailMessage getSendMessage(EmailMessage message) {
        if(message.getTime() == null || message.getTime() == 0){
            return null;
        }
        if(message.getTime() == 1){
            String content = "旭宏邮件提醒：" + message.getApplyName() + "申请了" + message.getApplyType()
                    + "报销，金额为：" + message.getApplyMoney() +"员，请您及时沟通处理。详细请登录旭宏办公系统查看";
            message.setContent(content);
            message.setReceivers(LIUQH);
        }
        if(message.getTime() == 2){
            String content = "旭宏邮件提醒：" + message.getApplyName() + "申请了" + message.getApplyType()
                    + "报销，金额为：" + message.getApplyMoney() +"员，业务经理已审核通过，请您及时沟通处理。详细请登录旭宏办公系统查看";
            message.setContent(content);
            message.setReceivers(SHIYP);
        }
        if(message.getTime() == 3){
            String content = "旭宏邮件提醒：" + message.getApplyName() + "申请了" + message.getApplyType()
                    + "报销，金额为：" + message.getApplyMoney() +"元，业务经理和人事经理已审核通过，请您及时沟通处理";
            message.setContent(content);
            message.setReceivers(YANQB);
        }
        if(message.getTime() == 4){
            String content = "旭宏邮件提醒：由" + message.getApplyName() + "申请的" + message.getApplyType()
                    + "报销，金额为：" + message.getApplyMoney() +"元，已经发放请注意查收。";
            message.setContent(content);
            message.setReceivers(LIUQH + "," + SHIYP + "," + YANQB + "," + message.getApplyEmail());
        }
        return message;
    }

}
